package com.example.asus.a17376037_zq_market;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import util.HttpUtil;
public class RegisteredActivity extends Activity implements View.OnClickListener{
    EditText et_id,et_telephone,et_password1,et_confirm,et_address;
    Button btn_register;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_registered);
        init();
    }

    private void init() {
        // TODO Auto-generated method stub
        btn_register=findViewById(R.id.btn_register1);
        et_id=findViewById(R.id.et_idd);
        et_telephone=findViewById(R.id.et_telephone);
        et_password1=findViewById(R.id.et_password1);
        et_confirm= findViewById(R.id.et_confirm);
        et_address=findViewById(R.id.et_address);
        Log.d("onClick", "jjj");
        btn_register.setOnClickListener(this);

    }

    @Override
    public void onClick(View arg0) {
        Log.d("RegisteredActivity", "56454566");
        Toast.makeText(this,"发送中.....",Toast.LENGTH_SHORT).show();
            int id = Integer.parseInt(et_id.getText().toString());
            String password = et_password1.getText().toString();
            String password2 = et_confirm.getText().toString();
            int telephone = Integer.parseInt(et_telephone.getText().toString());
            String address = et_address.getText().toString();
        if(password2.equals(password)){
            String path = "http://106.13.133.171:80/MarketServer/AddUser?id="
                    +id+"&password="+password+"&telephone="+telephone+"&address="+address;

            HttpUtil.sendHttpRequest(path, new Callback() {

                @Override
                public void onResponse(Call arg0, Response response) throws IOException {
                    // TODO Auto-generated method stub
                    String responseData = response.body().string();
                    showText(responseData);
                    Log.d("RegisteredActivity",responseData);
                }

                private void showText(final String aa) {
                    // TODO Auto-generated method stub
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            Toast.makeText(getApplicationContext(),aa, Toast.LENGTH_SHORT).show();
                            Intent in=new Intent(getApplicationContext(), LonginActivity.class);
                            startActivity(in);
                        }
                    });

                }

                @Override
                public void onFailure(Call arg0, IOException arg1) {
                    // TODO Auto-generated method stub
                    Log.d("RegisteredActivity","失败");
                }
            });

        }
        else{
            Toast.makeText(getApplicationContext(),"两次密码输入不一致",Toast.LENGTH_SHORT).show();
        }

    }
    }

